package com.amado.course40Reflect.homeworktwo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TestProxyTwoLever {
	public static void main(String[] args) {
		// 被代理对象
		CateService cateService = new CateServiceImpl();
		// 代理对象 参数1 当前类的类加载器用，参数二：要代理的对象，参数在handler
		CateService proxyObject = (CateService) Proxy.newProxyInstance(TestProxyTwoLever.class.getClassLoader(),
				new Class[] { CateService.class }, new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						long beginTime = System.currentTimeMillis();
						// 被代理对象的方法调用
						Object value = method.invoke(cateService, args);
						long endTime = System.currentTimeMillis();
						System.out.println("执行时间" + (endTime - beginTime));
						return value;
					}
				});

		// 代理对象 参数1 当前类的类加载器用，参数二：要代理的对象，参数在handler
		CateService proxyObjectTwo = (CateService) Proxy.newProxyInstance(TestProxyTwoLever.class.getClassLoader(),
				new Class[] { CateService.class }, new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						// 被代理对象的方法调用
						Object value = method.invoke(proxyObject, args);
						System.out.println("返回值" + value);
						return value;
					}
				});
		// 调用代理对象
		proxyObjectTwo.get("abc");
	}
}
